package com.learner.ucenter.controller.api;

import com.learner.pojo.user.SensitiveMessage;
import com.learner.pojo.user.vo.SensitiveMessageResultVo;
import com.learner.ucenter.filter.SensitiveFilter;
import com.learner.ucenter.service.SensitiveMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author linweibing
 * @date 2023-03-28 19:46
 * @description: TODO
 */
@RequestMapping("/api/ucenter/sensitiveMessage")
public class SensitiveMessageController {

    @Autowired
    SensitiveFilter sensitiveFilter;

    @Autowired
    SensitiveMessageService sensitiveMessageService;

    /**
     * 消息过滤
     *
     * @param sensitiveMessage 验证消息
     * @return String[]
     */
    @PostMapping("/filterMessage")
    @ResponseBody
    public String filterMessage(@RequestBody SensitiveMessage sensitiveMessage) {
        String[] res = sensitiveFilter.filter(sensitiveMessage.getMessage());
        String filterContent = "";
        if (res != null) {
            filterContent = res[0];
            if ("1".equals(res[1])) {
                //判断出敏感词，插入到敏感词表中
                sensitiveMessageService.addSensitiveMessage(sensitiveMessage);
            }
        }
        return filterContent;
    }

    /**
     * 获取敏感消息列表
     *
     * @return 结果
     */
    @GetMapping("/getSensitiveMessageList")
    @ResponseBody
    public List<SensitiveMessageResultVo> getSensitiveMessageList() {
        return sensitiveMessageService.getSensitiveMessageList();
    }

}
